package giotto2D.filters.color;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import giotto2D.filters.AbstractFilter;
import java.awt.*;
import java.awt.image.*;

public class StripeTone extends AbstractFilter
{

	//--- Color1 bg, Color2 drawColor
	public static void filter( 	BufferedImage img,
					Color color1,
					Color color2,
					int stripeHeight )
							
	{
		//--- Get raster 
		WritableRaster drawRaster = img.getRaster();

		//-- only odd values => 3 accepted as stripe height
		if( stripeHeight < 3 ) stripeHeight = 3;
		if( ( stripeHeight % 2 ) == 0 ) stripeHeight = stripeHeight + 1;

		//--- get rgb components of drawing colors.
		int r1 = color1.getRed();
		int g1 = color1.getGreen();
		int b1 = color1.getBlue();
		int r2 = color2.getRed();
		int g2 = color2.getGreen();
		int b2 = color2.getBlue();

		//--- Stripes count
		int stripes = img.getHeight() / stripeHeight;

		//--- Last Stripe width
		int lastStripeHeight = img.getHeight() % stripeHeight;
		if( lastStripeHeight != 0 ) stripes = stripes + 1;

		//--- Draw variables.
		int[] pixel = new int[ 4 ];
		int i, j, k;
		int yOffset;
		float stripeValueMax = 255 * stripeHeight * 3;
		int halfStripe = (stripeHeight / 2);
		float stripeValue;
		float drawValue;
		int drawHeight;
		//int startPix = halfStripe + 1;
		int stripeStart, stripeEnd;


		//--- Draw gradient 1 row at time
		for( i = 0; i < stripes; i++ )
		{
			yOffset = i * stripeHeight;
			for( j = 0; j < img.getWidth(); j++ )
			{ 
				//--- Set draw height for last handle lastStripe
				if( i == ( stripes - 1 ) && lastStripeHeight != 0 ) 
									stripeHeight = lastStripeHeight;
				
				//--- calculate stripe value
				stripeValue = 0;
				for( k = 0; k < stripeHeight; k++ )
				{	
				 	drawRaster.getPixel( j, yOffset + k, pixel );
					stripeValue = stripeValue+pixel[ RED ]+pixel[ GREEN ]+pixel[ BLUE ];
				}
				
				//--- calculate draw value and drawIterations
				drawValue =  stripeValue / stripeValueMax;
				drawHeight = Math.round( drawValue * stripeHeight );

				//--- Odd and even mount of draw pixels handle different
				stripeStart = halfStripe - ( drawHeight/ 2 );
				stripeEnd = stripeStart + drawHeight;//exclusive
		
				if( drawHeight == 0 ) stripeStart = stripeHeight;

				for( k = 0; k < stripeHeight; k++ )
				{	
					drawRaster.getPixel( j, yOffset + k, pixel );
					if( k < stripeStart )
					{
						pixel[ RED ] = r1;
						pixel[ GREEN ] = g1;
						pixel[ BLUE ] = b1;
					}

					else if( k < stripeEnd )
					{
						pixel[ RED ] = r2;
						pixel[ GREEN ] = g2;
						pixel[ BLUE ] = b2;
					}
					else
					{
						pixel[ RED ] = r1;
						pixel[ GREEN ] = g1;
						pixel[ BLUE ] = b1;
					}
					drawRaster.setPixel( j, yOffset + k, pixel );
				}
			}
		}
	}

}//end class
